package com.kugua.zfms.shardjdbc;

import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory;
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>TODO</p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author cuiyh9
 * @version 1.0
 * @Date Created in 2018年12月18日 20:28
 * @since 1.0
 */
public class ReadWriteDemo {

    public static void main(String[] args) throws Exception {
        ReadWriteDemo readWriteDemo = new ReadWriteDemo();
        readWriteDemo.executeRead();

    }

    public void executeRead() throws Exception {

        DataSource ds = getDataSource();

        Connection connection = ds.getConnection();
//      强制走主库
//        HintManager hintManager = HintManager.getInstance();
//        hintManager.setMasterRouteOnly();
        PreparedStatement ps = connection.prepareStatement("select * from test ");
        ResultSet resultSet = ps.executeQuery();
        try {
            while (resultSet.next()) {
                System.out.println(resultSet.getInt("id") + "  " + resultSet.getString("name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public DataSource getDataSource() {

//        // 配置真实数据源
//        // 配置第一个数据源
        BasicDataSource masterDataSource = new BasicDataSource();
        masterDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        masterDataSource.setUrl("jdbc:mysql://localhost:3306/db_0?useUnicode=true&serverTimezone=UTC&characterEncoding=utf-8&autoReconnect=true&useSSL=false");
        masterDataSource.setUsername("root");
        masterDataSource.setPassword("ziroom");
//        // 配置第二个数据源
        BasicDataSource dataSource1 = new BasicDataSource();
        dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource1.setUrl("jdbc:mysql://localhost:3306/db_1?useUnicode=true&serverTimezone=UTC&characterEncoding=utf-8&autoReconnect=true&useSSL=false");
        dataSource1.setUsername("root");
        dataSource1.setPassword("ziroom");

        // 构建读写分离数据源, 读写分离数据源实现了DataSource接口, 可直接当做数据源处理. masterDataSource, slaveDataSource0, slaveDataSource1等为使用DBCP等连接池配置的真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        dataSourceMap.put("masterDataSource", masterDataSource);
        dataSourceMap.put("slaveDataSource1", dataSource1);


        MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration();
        masterSlaveRuleConfig.setName("ms_ds");
        masterSlaveRuleConfig.setMasterDataSourceName("masterDataSource");
        masterSlaveRuleConfig.getSlaveDataSourceNames().add("slaveDataSource1");

        // 获取数据源对象
        DataSource dataSource = null;
        try {
            dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig , new HashMap<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;

    }
}
